package com.talos.ums.controller.admin.useracounttype;

import com.talos.framework.apilog.core.annotation.ApiAccessLog;
import com.talos.framework.common.pojo.CommonResult;
import com.talos.framework.common.pojo.PageParam;
import com.talos.framework.common.pojo.PageResult;
import com.talos.framework.common.util.object.BeanUtils;
import com.talos.framework.excel.core.util.ExcelUtils;
import com.talos.ums.controller.admin.useracounttype.vo.UmsUserAcountTypePageReqVO;
import com.talos.ums.controller.admin.useracounttype.vo.UmsUserAcountTypeRespVO;
import com.talos.ums.controller.admin.useracounttype.vo.UmsUserAcountTypeSaveReqVO;
import com.talos.ums.dal.dataobject.useracounttype.UmsUserAcountTypeDO;
import com.talos.ums.service.useracounttype.UmsUserAcountTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.talos.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.talos.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 账号类型")
@RestController
@RequestMapping("/ums/user-acount-type")
@Validated
public class UmsUserAcountTypeController {

    @Resource
    private UmsUserAcountTypeService userAcountTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建账号类型")
    @PreAuthorize("@ss.hasPermission('ums:user-acount-type:create')")
    public CommonResult<Long> createUserAcountType(@Valid @RequestBody UmsUserAcountTypeSaveReqVO createReqVO) {
        return success(userAcountTypeService.createUserAcountType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新账号类型")
    @PreAuthorize("@ss.hasPermission('ums:user-acount-type:update')")
    public CommonResult<Boolean> updateUserAcountType(@Valid @RequestBody UmsUserAcountTypeSaveReqVO updateReqVO) {
        userAcountTypeService.updateUserAcountType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除账号类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ums:user-acount-type:delete')")
    public CommonResult<Boolean> deleteUserAcountType(@RequestParam("id") Long id) {
        userAcountTypeService.deleteUserAcountType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得账号类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ums:user-acount-type:query')")
    public CommonResult<UmsUserAcountTypeRespVO> getUserAcountType(@RequestParam("id") Long id) {
        UmsUserAcountTypeDO userAcountType = userAcountTypeService.getUserAcountType(id);
        return success(BeanUtils.toBean(userAcountType, UmsUserAcountTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得账号类型分页")
    @PreAuthorize("@ss.hasPermission('ums:user-acount-type:query')")
    public CommonResult<PageResult<UmsUserAcountTypeRespVO>> getUserAcountTypePage(@Valid UmsUserAcountTypePageReqVO pageReqVO) {
        PageResult<UmsUserAcountTypeDO> pageResult = userAcountTypeService.getUserAcountTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UmsUserAcountTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出账号类型 Excel")
    @PreAuthorize("@ss.hasPermission('ums:user-acount-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserAcountTypeExcel(@Valid UmsUserAcountTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UmsUserAcountTypeDO> list = userAcountTypeService.getUserAcountTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "账号类型.xls", "数据", UmsUserAcountTypeRespVO.class,
                        BeanUtils.toBean(list, UmsUserAcountTypeRespVO.class));
    }

}
